#!/bin/bash
##
##第一个参数：运行环境，pro或test
##第二个参数：生成zip包地址
##第三个参数：是否执行mvn test，true或false
##第四个参数：车场名称，qingqi，yiqi，dongfeng
##
CUR_DIR=$(cd "$(dirname "${BASH_SOURCE-$0}")"; pwd)

BASE_PATH=${CUR_DIR}

AERO_DATA_PLATFORM_SRC_PATH="${BASE_PATH}/../../spark-applications/aero-data-platform"
TRIP_ANALYSIS_COMMON_SRC_PATH="${BASE_PATH}/../../spark-applications/opentsp-location-tripanalysis/opentsp-location-tripanalysis-common"
TRIP_ANALYSIS_SCORE_SRC_PATH="${BASE_PATH}/../../spark-applications/opentsp-location-tripanalysis/opentsp-location-tripanalysis-score"
TRIP_ANALYSIS_APP_SRC_PATH="${BASE_PATH}/../../spark-applications/opentsp-location-tripanalysis/opentsp-location-tripanalysis-offline"
SUMMARYDAILY_APP_SRC_PATH="${BASE_PATH}/../../spark-applications/opentsp-location-summary-daily"
ANALYSYS_APP_SRC_PATH="${BASE_PATH}/../../spark-applications/opentsp-location-analysis"

folderName=data_platform_job
export theme=$1

if [ $# -ge 4 ]; then
    echo "Shell 传递参数$#个！"
	echo "执行的文件名：$0"
	echo "第一个参数为：$1"
	echo "第二个参数为：$2"
	echo "第三个参数为：$3"
	echo "第四个参数为：$4"
else
    echo "参数错误！"
    echo "Shell 传递参数$#个！"
    echo "第一个参数为：运行环境pro or test"
    echo "第二个参数为：生成zip包的地址"
    echo "第三个参数为：是否执行mvn test"
    echo "第四个参数为：打包哪个车场的部署包"
    exit 1
fi

if [ -n "$2" ];then
    OUT_PATH=$2
else
   OUT_PATH=${BASE_PATH}/../../../
fi

YARD=$4
#如果第三个参数为false，则不进行maven clean，直接生成布署包
if [ "false" == "$3" ];then
  mvn_package="&& mvn package -Dmaven.test.skip=true -P ${YARD}"
  mvn_install="&& mvn install -Dmaven.test.skip=true -P ${YARD}"
else
  mvn_package="&& mvn clean package -P ${YARD}"
  mvn_install="&& mvn clean install -P ${YARD}"
fi



ETL_PATH=${BASE_PATH}/../../etl-scripts/data-warehouse/${YARD}
ETL_DAY_WEEK_MONTH_PATH="${ETL_PATH}/hive_to_mysql/"
ETL_KUDU=${ETL_PATH}/other_to_kudu
#初始化输出文件夹
OUT_PATH=${OUT_PATH}/${theme}
if [ -d "${OUT_PATH}" ]
then
	rm -rf ${OUT_PATH}
fi
mkdir -p ${OUT_PATH}

#打包spark程序到指定文件夹
mvnPackageApp(){
    srcBasePath=$1
    appName=$2
    str1="cd ${srcBasePath} ${mvn_package} && unzip target/$(ls -alh ${srcBasePath}/target/ |grep zip |grep ${appName} |awk '{print $9}') -d ${OUT_PATH}/${folderName}/"
    echo "===============================================>"$str1
    bash -c "$str1"
    #改名
    mv ${OUT_PATH}/${folderName}/$(ls -alh ${OUT_PATH}/${folderName}/ |grep ${appName} |awk '{print $9}') ${OUT_PATH}/${folderName}/${appName}

    #bin目录加上可执行权限
    chmod -R 755 ${OUT_PATH}/${folderName}/${appName}/bin
}
#依赖库打包
mvnInstallDep(){
    srcBasePath=$1
    str1="cd ${srcBasePath} ${mvn_install} "
    echo "===============================================>"$str1
    bash -c "$str1"
}


echo "swtichEnv:${theme}"
echo "OUT_PATH:${OUT_PATH}"

#将代码复制到输出文件夹
cp -rf "${BASE_PATH}/${YARD}/${folderName}" ${OUT_PATH}/

# 驾驶行为平台
mvnInstallDep "${TRIP_ANALYSIS_COMMON_SRC_PATH}"
mvnInstallDep "${TRIP_ANALYSIS_SCORE_SRC_PATH}"
mvnPackageApp "${TRIP_ANALYSIS_APP_SRC_PATH}" "tripanalysis-offline"

# 统计分析平台
mvnPackageApp "${AERO_DATA_PLATFORM_SRC_PATH}" "aero-data-platform"

# 里程油耗转存到mongo
mvnPackageApp "${SUMMARYDAILY_APP_SRC_PATH}" "summary-daily"

# 区域车次统计
mvnPackageApp "${ANALYSYS_APP_SRC_PATH}" "location-analysis"

#kudu etl
echo "=====================================================cp -rf ${ETL_KUDU} ${OUT_PATH}/${folderName}/"
cp -rf ${ETL_KUDU} ${OUT_PATH}/${folderName}/

#替换配置
cp -rf ${BASE_PATH}/${YARD}/config_env/${folderName}/${theme}/* "${OUT_PATH}/${folderName}/"

#拷贝配置文件
cp -rf ${ETL_DAY_WEEK_MONTH_PATH}/*.sh ${OUT_PATH}/${folderName}/sh/shellFiles/run/export_hive_mysql/

zip_time=`date +%Y%m%d%H%M%S`
zip_file_name=${YARD}_${folderName}_${theme}_${zip_time}.zip
#打包成zip格式
bash -c  "cd ${OUT_PATH} && zip -r ${OUT_PATH}/${zip_file_name} ${folderName}/*"

#输出打包后的文件路径
echo "${OUT_PATH}/${zip_file_name}"
ls -alh ${OUT_PATH}/

